<template>
  <div style="width:100%;padding: 20px;background: #fff">
    <h2>{{tableTitle}}</h2>
    <div class="export">
      <el-button @click="exportExcel" size="medium" type="primary">导出</el-button>
    </div>
    <el-table
      id="sankeyTalbe"
      :data="tableData"
      fit
      border
      :max-height="500"
      :span-method="arraySpanMethod"
      stripe
      style="width:100%;margin-top: 50px"
    >
      <el-table-column prop="formSource" label="导流店铺" align="center"></el-table-column>
      <el-table-column prop="formValue" label="导流客流" align="center"></el-table-column>
      <el-table-column prop="" :label="tableData[0] && tableData[0].formTarget ? tableData[0].formTarget :
                                        tableData[0] && tableData[0].toSource ? tableData[0].toSource : ''"
                       width="120" align="center" ></el-table-column>
      <el-table-column prop="toTarget" label="被导流店铺" align="center"></el-table-column>
      <el-table-column prop="toValue" label="被导流客流" align="center"></el-table-column>

    </el-table>
  </div>

</template>

<style lang="scss">
  .export{
    float: right;
    height: 30px;
    line-height: 30px;
    margin-left: 5px;
    .el-button{
      padding: 0 15px;
      margin-top:1px;
      height: 30px;
      line-height: 30px;
      &.editbtn{
        border: 1px solid #cad7ff;
        background: #f2f5ff;
        color:#5f67ec;
        &:hover,&:focus{
          color: #FFFFFF;
          background-color: #545deb;
          border-color: #545deb;
        }
      }
    }

  }
  .el-table {
    color:#838ca1;
    .el-table__header-wrapper,
    .el-table__fixed,
    .el-table__fixed-right,
    .el-pagination,
    .el-table__fixed-header-wrapper {
      .has-gutter,
      thead {
        tr {
          th {
            background: #f5f6fa;
            color: #000a2c;
            font-weight: 400;
            line-height: 50px;
            padding-top: 0;
            padding-bottom: 0;

          }
        }
      }

    }
    .el-table__fixed-right-patch {
      border: none;
      background: #f5f6fa;
    }
    tbody {
      .el-table__row.nulldata,.el-table__row--striped.el-table__row.nulldata{
        background-color: bisque;
        td {
          background: bisque;
        }
        &.hover-row{
          &>td{
            background-color: bisque;
          }
        }
      }
      .td-max-min{
        display: inline-block;
        border-radius: 16px;
        overflow: hidden;
        height: 32px;
        vertical-align: middle;
        .maxdiv{
          position: relative;
          background: #F56C6C;
          // &::after{
          //   content: 'Max';
          //   background-color: #F56C6C;
          //   position: absolute;
          //   top: 0;
          //   right: 15px;
          //   transform: translateY(-50%) translateX(100%);
          //   border-radius: 10px;
          //   color: #FFF;
          //   display: inline-block;
          //   font-size: 12px;
          //   height: 18px;
          //   line-height: 18px;
          //   padding: 0 6px;
          //   text-align: center;
          //   white-space: nowrap;
          //   border: 1px solid #FFF;
          // }
        }
        .mindiv{
          position: relative;
          background: #67C23A;

          // &::after{
          //   content: 'Min';
          //   background-color: #67C23A;
          //   position: absolute;
          //   top: 0;
          //   right: 15px;
          //   transform: translateY(-50%) translateX(100%);
          //   border-radius: 10px;
          //   color: #FFF;
          //   display: inline-block;
          //   font-size: 12px;
          //   height: 18px;
          //   line-height: 18px;
          //   padding: 0 6px;
          //   text-align: center;
          //   white-space: nowrap;
          //   border: 1px solid #FFF;
          // }
        }
        .td-max-span{
          display: inline-block;
          height: 32px;
          line-height: 32px;
          padding: 0px 20px;
          font-size: 14px;
          color:#fff;
        }
        .td-min-span{
          display: inline-block;
          height: 32px;
          line-height: 32px;
          padding: 0px 20px;
          font-size: 14px;
          color:#fff;
        }
      }

      .el-table__row {
        // &:hover {
        //   td {
        //      background: #f5f7fa;
        //   }
        // }
        td {
          line-height: 46px;
          height: 46px;
          /*div {*/
          /*line-height: 23px;*/
          /*}*/
          padding: 0;
        }
      }
      .el-table__row--striped {
        // &:hover {
        //   td {
        //      background: #e3efff;
        //   }
        // }
        td {
          background: #f9fafe;
        }
        &.hover-row{
          &>td{
            background: #F5F7FA;
          }
        }
      }
    }
    .el-table__body-wrapper {
      &::-webkit-scrollbar {
        width: 18px;
        height: 18px;
        //scrollbar-arrow-color: red;
      }

      /*定义滚动条轨道 内阴影+圆角*/
      &::-webkit-scrollbar-track {
        box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 10px;
      }

      /*定义滑块 内阴影+圆角*/
      &::-webkit-scrollbar-thumb {
        border-radius: 10px;
        box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        background: rgba(0, 0, 0, 0.2);
        //scrollbar-arrow-color: red;
      }
    }
  }
</style>

<script>
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'
  export default {
    props:{
      tableData:{
        default:function () {
          return [{formTarget : ''}]
        }
      },
      tableTitle:{
        default:function () {
          return ''
        }
      }
    },
    data() {
      return {
      }
    },
    methods:{
      arraySpanMethod({ row, column, rowIndex, columnIndex }){
        if (rowIndex === 0) {
          if (columnIndex === 2) {
            return [this.tableData.length,1]
          }
        }else{
          if (columnIndex === 2) {
            return [0,0]
          }
        }
      },
      //导出表格
      exportExcel() {
        /* generate workbook object from table */
        let id = '#sankeyTalbe';
        let wb;
        let fix = document.querySelector('.el-table__fixed');
        if (fix) {
          wb = XLSX.utils.table_to_book(document.querySelector(id).removeChild(fix));
          document.querySelector(id).appendChild(fix);
        } else {
          wb = XLSX.utils.table_to_book(document.querySelector(id));
        }
        /* get binary string as output */
        let wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: true, type: 'array'});
        let name = this.tableTitle

        try {
          FileSaver.saveAs(new Blob([wbout], {type: 'application/octet-stream'}), name + '.csv');
        } catch (e) {
          if (typeof console !== 'undefined') console.log(e, wbout)
        }
        return wbout
      },
    }
  }
</script>

